package com.zzk.im.tool;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.SimpleDateFormat;


public class JsonTool {

    private static ObjectMapper mapper;

    static {
        mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, false);
        mapper.configure(SerializationFeature.WRAP_EXCEPTIONS, true);
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,
                false);
        mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES,
                false);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

    }

    public static String write(Object data) throws Exception {
        if (data == null) {
            return null;
        }
        return mapper.writeValueAsString(data);
    }

    public static Object read(String json, Class<?> clz) throws Exception {
        if (json == null || json.equals("") || clz == null) {
            return null;
        }
        return mapper.readValue(json, clz);
    }

}